﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace IconEditor.IconElements
{
    public enum LinePattern { Solid };
    public enum FillPattern { None, Solid };
    public enum BorderPattern { None };
    public enum Arrow { None };
    public enum TextStyle { None };

    public class BasicShape : UserControl
    {
        protected Point mousePosition;
        protected bool isMouseCaptured;

        public BasicShape()
        {
            this.MouseEnter += new MouseEventHandler(BasicShape_MouseEnter);
            this.MouseLeave += new MouseEventHandler(BasicShape_MouseLeave);
        }

        protected virtual void BasicShape_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement item = sender as FrameworkElement;
            mousePosition = e.GetPosition(null);
            isMouseCaptured = true;
            item.CaptureMouse();
            item.Cursor = Cursors.Hand;
        }

        protected virtual void BasicShape_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement item = sender as FrameworkElement;
            mousePosition.X = mousePosition.Y = 0;
            isMouseCaptured = false;
            item.ReleaseMouseCapture();
            item.Cursor = null;
        }

        protected virtual void BasicShape_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement item = sender as FrameworkElement;
            // Calculate the current position of the object.
            double deltaV = e.GetPosition(null).Y - mousePosition.Y;
            double deltaH = e.GetPosition(null).X - mousePosition.X;
            double newTop = deltaV + Canvas.GetTop(item);
            double newLeft = deltaH + Canvas.GetLeft(item);

            // Set new position of object.
            Canvas.SetTop(item, newTop);
            Canvas.SetLeft(item, newLeft);

            // Update position global variables.
            mousePosition = e.GetPosition(null);
        }

        protected virtual void Resize_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseCaptured)
            {
                double deltaVertical, deltaHorizontal;
                FrameworkElement item = sender as FrameworkElement;

                // Calculate the current position of the object.
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                double deltaH = e.GetPosition(null).X - mousePosition.X;

                switch (item.VerticalAlignment)
                {
                    case System.Windows.VerticalAlignment.Bottom:
                        deltaVertical = Math.Min(deltaV, this.ActualHeight - this.MinHeight);
                        this.Height += deltaVertical;
                        break;
                    case System.Windows.VerticalAlignment.Top:
                        deltaVertical = Math.Min(deltaV, this.ActualHeight - this.MinHeight);
                        Canvas.SetTop(this, Canvas.GetTop(this) + deltaVertical);
                        this.Height -= deltaVertical;
                        break;
                    default:
                        break;
                }

                switch (item.HorizontalAlignment)
                {
                    case System.Windows.HorizontalAlignment.Left:
                        deltaHorizontal = Math.Min(deltaH, this.ActualWidth - this.MinWidth);
                        Canvas.SetLeft(this, Canvas.GetLeft(this) + deltaHorizontal);
                        this.Width -= deltaHorizontal;
                        break;
                    case System.Windows.HorizontalAlignment.Right:
                        deltaHorizontal = Math.Min(deltaH, this.ActualWidth - this.MinWidth);
                        this.Width += deltaHorizontal;
                        break;
                    default:
                        break;
                }
                mousePosition = e.GetPosition(null);
            }
        }

        protected void BasicShape_MouseEnter(object sender, MouseEventArgs e)
        {
            ShowControls();
        }

        protected void BasicShape_MouseLeave(object sender, MouseEventArgs e)
        {
            HideControls();
        }

        protected virtual void ShowControls()
        {
        }

        protected virtual void HideControls()
        {
        }

        internal virtual string GetAnnotation()
        {
            return "";
        }
    }
}
